309B - Context Advertising - CodeForces Solution


dp two pointers *2100

Please click on ads to support us..

C++ Code:

    #include <bits/stdc++.h>
    using namespace std;

    using ll = long long;
    using pll = pair<long long, long long>;

    #define pb push_back
    #define F first
    #define S second  
    #define all(x) (x)  .begin(), (x).end()
    
    const ll N = 1e6 + 100;
    const ll inf = 1e18;
    const ll mod = 1e9 + 7;
    const ll block = 520;
    const ll base = 35711;
    ll a[N], ps[N], up[N][25], nxt[N];
    string s[N];
    ll n,r,c;
    void build(){
        for(int j = 0; j <= 24;j++) up[n+1][j] = n + 1;
        for(int j = 1; j <= 24;j++){
            for(int i = 1; i <= n;i++){
                up[i][j] = up[up[i][j-1]][j-1];
            }
        }
    }
    void to_nho_cau(){
        cin >> n >> r >> c;
        for(int i = 1; i <= n;i++){
            cin >> s[i];
            ps[i] = ps[i-1] + s[i].size();  
        }
        for(int i = 1; i <= n;i++){
            ll l = i, r = n + 1;
            while(l + 1 < r){
                ll mid = (l + r) / 2;
                if(ps[mid] - ps[i - 1] + (mid - i) <= c) l = mid;
                else r = mid;
            }
            if(s[i].size() > c){
                up[i][0] = i;
            }   
            else{
                up[i][0] = l + 1;
            }
        }
        build();
        ll res = 0;
        for(int i = 1; i <= n;i++){
            ll u = i;
            for(int j = 24; j >= 0;j--) if(r & (1 << j)) u = up[u][j];
            nxt[i] = --u;
            if(s[i].size() <= c) res = max(res, u - i + 1);
        }
        for(int i = 1; i <= n;i++){
            if(nxt[i] - i + 1 == res){
                ll cnt = 0;
                for(int j = i; j <= nxt[i];j++){
                    if(cnt + s[j].size() > c){
                        cnt = 0;    
                        cout << '\n';
                        r--;
                        if(!r) return;
                    }
                    else if(cnt) cout << ' ';
                    cout << s[j];
                    cnt += s[j].size() + 1;
                }
                return;
            }
        }
    }

    signed main()
    {   
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        int t = 1;   
        //cin >> t;
        while(t--) to_nho_cau();
    }


Comments

Submit
0 Comments
More Questions

742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing